<?php
namespace app\admin\controller;
use think\Validate;
use think\Db;
class Service extends SessionCheck
{
    public function index()
    {
		$gid = input('get.gid');
		$cateid = input('get.cateid');
		$aid = input('get.aid');
		$title = input('get.title');
		$where = [];
		if(!empty($aid)){
			$where['aid'] = $aid;
		}
		if(!empty($gid)){
			$wher['gid'] = $gid;
		}
		if(!empty($cateid)){
		    $where['cateid'] = $cateid;
        }
		if(!empty($title)){
			$where['title'] = ['like','%'.$title.'%'];
		}
		$pages = pages('service',$where,input('get.page'));
		$data = Db::table("service")->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order("replace(flag,'0','desc'),id desc")->select();
        foreach ($data as $k => $v){
            $data[$k]['category'] = Db::table('service_category')->where('id',$v['cateid'])->value('title');
        }
		return view('',['data'  => $data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }
	
	public function edit()
    {
		$id = input("get.id")?input("get.id"):0;
		if(request()->isPost()){
			$data = input("post.");
			/*$validate = new Validate([
				['title','require',lang('label')['title'].lang('currency')['required']],
				['price','require',lang('label')['price'].lang('currency')['required']],
				['service','require',lang('label')['service'].lang('currency')['required']],
				['content','require',lang('label')['content'].lang('currency')['required']],
			]);
			if (!$validate->check($data)) {
				return error($validate->getError());
			}*/
//			$seller = Db::table("user")->where(['type'=>1,'id' => $data['uid']])->whereIn('aid',session('aid'))->find();
			/*$data['aid'] = $seller['aid'];
			$data['aid'] = $seller['aid'];
			$data['aid'] = $seller['aid'];*/
//            $data['comid'] = Db::table('user')->where('id',$data['uid'])->value('comid');
			if($id){
				Db::table("service")->where(['id' => $id])->whereIn('aid',session('aid'))->update($data);
				$data['id'] = $id;
			}else{
				$data['time'] = time();
				$data['id'] = Db::table("service")->insertGetId($data);
			}
		}
		$data = Db::table("service")->where(['id' => $id])->whereIn('aid',session('aid'))->find();
//		$seller = Db::table("user")->where(['type'=>1,'status'=>1])->whereIn('aid',session('aid'))->order('id desc')->select();
        return view('',['data'  => $data,'menu_list'=>$this->menu_list]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		Db::table("service")->whereIn('aid',session('aid'))->delete($id);
		$this->redirect(url('/admin/service').'?page='.$page);
		exit;
    }
	
	public function flag()
    {
		$id = input("get.id");
		$flag = input("get.flag");
		$flagtime = input("get.flagtime");
		if(!$id){
			$return = array(
				'code' => 'error',
				'msg' => "id".lang('currency')['error']
			);
			return $return;
		}
		if($flagtime){
			$flagtime = strtotime($flagtime)+86399;
		}
		$update['flag'] = $flag;
		$update['flagtime'] = $flagtime;
		Db::table("service")->where(['id' => $id])->whereIn('aid',session('aid'))->update($update);
		$return = array(
			'code' => 'success',
			'msg' => lang('currency')['success']
		);
		return $return;
    }
	
	public function examine()
    {
		$pages = pages('service',['status'=>3],input('get.page'));
		$data = Db::table("service")->where(['status'=>3])->whereIn('aid',session('aid'))->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }
	
	public function setting()
    {
		if(request()->isPost()){
			$data = input("post.");
			Db::table("seller_config")->where(['id'=>1])->update($data);
		}
		$data = Db::table("seller_config")->where(['id'=>1])->find();
        return view('',['data' => $data]);
    }
	
	public function category()
    {
		$data = array();
		$category = Db::table("service_category")->where('gid',0)->order('id asc')->select();
		if(!empty($category)){
			foreach($category as $sub){
				$sub['sub'] = Db::table("service_category")->where('gid',$sub['id'])->order('id asc')->select();
				array_push($data,$sub);
			}
		}
        return view('',['data'  => $data,'menu_list'=>$this->menu_list]);
    }

    public function upload()
    {
        $config = Db::table('material_config')->where('id',1)->find();
        config([
            'UPLOAD_SITEIMG_QINIU'=>[
                'maxSize'          => 5 * 1024 * 1024,//文件大小
                'rootPath'         => './',
                'saveName'         => ['uniqid', ''],
                'driver'           => 'Qiniu',
                'driverConfig' => [
                    'secretKey' => $config['secretKey'],
                    'accessKey' => $config['accessKey'],
                    'domain'    => $config['domain'],
                    'bucket'    => $config['bucket'],
                ],
            ],
            'file_size'=>$config['file_size']
        ]);
        $setting=config('UPLOAD_SITEIMG_QINIU');
        $Upload = new \think\Upload($setting);
        $infos = $Upload->upload($_FILES);
        return $infos;
    }
	
	public function category_edit()
    {
		$id = input("get.id")?input("get.id"):0;
		if(request()->isPost()){
			$data = input("post.");
            if($_FILES['image']['tmp_name'] != ''){
                $image = $this->upload();
                $data['image'] = $image['image']['url'];
            }
			if(!empty($id)){
				Db::table("service_category")->where('id',$id)->update($data);
			}else{
				Db::table("service_category")->insert($data);
			}
			echo 'please wait...';
			return '<script>parent.location.reload();</script>';
		}else{
		    $gid = request()->request('gid');
		    $data = [];
		    if(!empty($id)){
		        $data = Db::table('service_category')->where('id',$id)->find();
            }
            if(empty($data['gid'])){
                $data['gid'] = $gid;
            }
            error_reporting(0);
		    return view('',['data'=>$data,'gid'=>$gid,'menu_list'=>$this->menu_list]);
        }
    }

    public function category_flag()
    {
        $id = input("get.id")?input("get.id"):0;
        $flag = input('get.flag');
        if($flag == 1){
            $flag = 0;
        }else{
            $flag = 1;
        }
        Db::table('service_category')->where('id',$id)->update(['flag'=>$flag]);
        $return = array(
            'code' => 'success',
            'msg' => lang('currency')['success']
        );
        return $return;
    }

    public function flag_edit()
    {
        $id = input("get.id")?input("get.id"):0;
        if(request()->isPost()){
            $data = request()->post();
            if($_FILES['image']['tmp_name'] != ''){
                $flag_image = $this->upload();
                $data['flag_image'] = $flag_image['image']['url'];
            }
            Db::table('service_category')->where('id',$id)->update($data);
            echo 'please wait...';
            return '<script>parent.location.reload();</script>';
        }else{
            $data = Db::table('service_category')->where('id',$id)->find();
            return view('',['data'=>$data,'menu_list'=>$this->menu_list]);
        }
    }
	
	public function category_del()
    {
		$id = input("get.id")?input("get.id"):0;
		Db::table("service_category")->delete($id);
		Db::table("service")->where('aid',$id)->delete();
		Db::table("service_category")->where('gid',$id)->delete();
		Db::table("service")->where('gid',$id)->delete();
		$return = array(
				'code' => 'success',
				'msg' => lang('currency')['success']
			);
		return $return;
    }
	
	public function ajax_get()
    {
		$gid = input('get.id')?input('get.id'):0;
		$data = Db::table("service_category")->where(['gid'=>$gid])->order('id asc')->select();
		$return = array(
			'data' => $data
		);
		return $return;
    }
}
